package com.hjs.servlet.regularinspection.regularinspection;

import com.hjs.bean.RegularInspectionView;
import com.hjs.mapper.ViewMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/RegularInspection/search")
@Slf4j
public class SearchRegularInspection extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        /*
         * 提供参数
         * departmentID
         */
        RequestUtil requestUtil = new RequestUtil(request,response);
        Mapper<ViewMapper> viewMapperMapper = new Mapper<>(ViewMapper.class);
        try {
            ViewMapper mapper = viewMapperMapper.getMapper();
            if (requestUtil.getString("departmentID") == null) {
                List<RegularInspectionView> allRegularInspectionView = mapper.getAllRegularInspectionView();
                requestUtil.responseWrite(Mapper.toJsonString(allRegularInspectionView));
            } else {
                List<RegularInspectionView> oneRegularInspectionView = mapper.getRegularInspectionViewByDepartmentId(requestUtil.getInt("departmentID"));
                requestUtil.responseWrite(Mapper.toJsonString(oneRegularInspectionView));
            }
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("查询失败");
        } finally {
            viewMapperMapper.closeSession();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

